package com.codeoss.framework.webide.controller;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.codeoss.framework.webide.request.ApplicationPageRequest;
import com.codeoss.framework.webide.response.ApplicationPageResponse;
import com.codeoss.framework.webide.service.IApplicationPageService;

@Controller
@RequestMapping("webide/applicationpage")
public class ApplicationPageController {
	@Autowired
	private IApplicationPageService applicationPageService;
	
	@RequestMapping(value = "init", method = RequestMethod.GET)
	@ResponseBody
	public ApplicationPageResponse init(){
		ApplicationPageResponse applicationPageResponse=applicationPageService.init();
		return applicationPageResponse;
	}
	
	@RequestMapping(value = "dataSave", method = RequestMethod.POST)
	@ResponseBody
	public ApplicationPageResponse dataSave(@RequestBody ApplicationPageRequest request){
		ApplicationPageResponse applicationPageResponse=applicationPageService.dataSave(request);
		return applicationPageResponse;
	}
	
	@RequestMapping(value = "dataExport", method = RequestMethod.GET)
	@ResponseBody
	public void dataExport(@RequestParam("code") String code,HttpServletResponse response)throws Exception{
		ApplicationPageResponse applicationPageResponse=applicationPageService.dataExport( code);
		response.reset();
		try {
			response.addHeader("Content-Disposition","attachment;filename="+new String((code+".zip").getBytes(),"ISO-8859-1"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		response.getOutputStream().write(applicationPageResponse.getByteArrayOutputStream().toByteArray());
	}
	
	@RequestMapping(value = "upload", method = RequestMethod.POST)
	@ResponseBody
	public ApplicationPageResponse  upload(@RequestParam("file") MultipartFile file)throws Exception{
		InputStream is=file.getInputStream();
		ApplicationPageResponse applicationPageResponse=applicationPageService.dataImport(is);
		return applicationPageResponse;
	}
	
	@RequestMapping(value = "dataDelete", method = RequestMethod.POST)
	@ResponseBody
	public ApplicationPageResponse dataDelete(@RequestParam("code") String code)throws Exception{;
		ApplicationPageResponse applicationPageResponse=applicationPageService.dataDelete(code);
		return applicationPageResponse;
	}
	
	@RequestMapping(value = "getApplicationIconContent", method = RequestMethod.GET)
	@ResponseBody
	public  void getApplicationIconContent(@RequestParam("icon") String icon,HttpServletResponse response)throws Exception{
		ApplicationPageResponse applicationPageResponse=applicationPageService.getApplicationIconContent(icon);
		if(applicationPageResponse.getApplicationIconContent()!=null) {
			response.getOutputStream().write(applicationPageResponse.getApplicationIconContent());
		}	
	}
}
